package com.extremelylargepackage.entity;

import java.io.Serializable;
import java.util.Map;

import android.graphics.Canvas;
import android.util.Log;

import com.extremelylargepackage.StatTracker;
import com.extremelylargepackage.entity.Character;
import com.extremelylargepackage.entity.Entity;
import com.extremelylargepackage.sprite.Animation;

public class Enemy extends Character implements Serializable
{
	
	protected transient Player _player;

	protected transient ArtificialIntelligence _ai;
	
	private transient Projectile _lastCollidedProjectile;

	int _collisionDamage = 0;
	 
	public Enemy()
	{	
		_status = Status.alive;
	}

	@Override
	public void updateState(long time) 
	{
		super.updateState(time);

		if(_status != Status.dead)
		{
			if(_status != Status.dying)
				_ai.performAi(time);
			move(time);
		}
	}

	@Override
	public void onCollision(Entity z) 
	{
		if(z instanceof Projectile && z._status == Status.alive && z != _lastCollidedProjectile)
		{
			Projectile proj = (Projectile)z;
			_health -= proj.getDamage();
			_lastCollidedProjectile = proj;
			StatTracker.getInstance().RegisterShotHit();
		}
		else if(z instanceof Player && z.getStatus() == Status.alive)
		{
			_health = -10;
		}
	}
	
	@Override
	public Object clone()
	{
		Enemy clone = new Enemy();
		clone._sprite = _sprite;
		clone._health = clone._maxHealth = _maxHealth;
		if(_weapon != null)
		{
			clone._weapon = (Weapon) _weapon.clone();
			clone._weapon._owner = clone;
		}
		clone._speed = _speed;
		clone._name = _name;
		clone._ai = (ArtificialIntelligence)_ai.clone();
		clone._ai.setEnemy(clone);
		if(_explosionAnim != null)
			clone._explosionAnim = (Animation)_explosionAnim.clone();
		clone._collisionDamage = _collisionDamage;
		return clone;
	}
	
	public void setPlayer(Player player)
	{
		_player = player;
	}

	public boolean hasWeapon()
	{
		return _weapon != null;
	}
	
	public boolean finished()
	{
		//We can stop updating this enemy if it is dead and the death animation
		//is finished or if the enemy has fled to an off screen location.
		return (_status == Status.dead || _ai.finished()) && (_weapon == null || !_weapon.hasActiveProjectiles());
	}

	public int getCollisionDamage()
	{
		return _collisionDamage;
	}

	@Override
	public Map<String, Object> getState()
	{
		Map<String, Object> res = super.getState();

		// add _lastCollidedProjectile?

		return res;
	}
	
	@Override
	public void draw(Canvas c)
	{
		super.draw(c);
	}
}
